#include "stm32f1xx.h"
#include "printf.h"



UART_HandleTypeDef huart1 = {
    .Instance = USART1,
    .Init.BaudRate = 115200,
    .Init.HwFlowCtl = UART_HWCONTROL_NONE,
    .Init.Mode = UART_MODE_TX_RX,
    .Init.OverSampling = UART_OVERSAMPLING_16,
    .Init.Parity = UART_PARITY_NONE,
    .Init.StopBits = UART_STOPBITS_1,
    .Init.WordLength = UART_WORDLENGTH_8B,

};

// PA9-TX PA10-RX
GPIO_InitTypeDef gpio_config = {
    .Pin = GPIO_PIN_9 | GPIO_PIN_10,
    .Mode = GPIO_MODE_AF_PP,
    .Pull = GPIO_PULLUP,
    .Speed = GPIO_SPEED_FREQ_HIGH};

    #ifdef PRINTF_VISIBILITY
#include "printf.h"

void putchar_(char c)
{
    while ((USART1->SR & USART_SR_TC) == 0)
    {
        /* code */
    }

    USART1->DR = (uint8_t)(c & 0xFFU);
    // HAL_UART_Transmit(&huart1, &c, 1, 100);
}
#endif

void uart1_init()
{
    __HAL_RCC_USART1_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    HAL_GPIO_Init(GPIOA, &gpio_config);

    HAL_UART_Init(&huart1);
    __HAL_UART_ENABLE(&huart1);

    printf_("uart1 init ok %ld\r\n", uwTick);
}

void uart1_send(uint8_t *data, uint32_t lenght)
{
    HAL_UART_Transmit(&huart1, data, lenght, 500);
}
